﻿<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>人员编辑</title>
    <script src="/Apm/js/CMSEdit.js"></script>
    <style>
       
    </style>
</head>
<body>
    <div class="page">
        <div id="title" class="title">
            
        </div>
        <div class="layui-form layui-form-pane edit-form">

            <div class="layui-form-item">
                <label class="layui-form-label">姓名</label>
                <div class="layui-input-block">
                    <input id="Name" type="text" name="Name" required lay-verify="required" placeholder="请输入姓名" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item" pane>
                <label class="layui-form-label">性别</label>
                <div class="layui-input-block">
                    <input id="Sex" type="checkbox" name="Sex" lay-text="男|女" value="1" checked lay-skin="switch">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">手机号</label>
                <div class="layui-input-block">
                    <input id="Phone" type="text" name="Phone" onchange="FnPhone()" required lay-verify="required|phone" placeholder="请输入手机号" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">所在组</label>
                <div class="layui-input-block">
                    <select id="GroupList" name="GroupCode" required lay-verify="required" lay-filter="GroupList">
                        <option value="">请选择分组</option>

                    </select>
                </div>
            </div>
            <div class="layui-form-item" pane>
                <label class="layui-form-label">用户状态</label>
                <div class="layui-input-block">
                    <input id="State" type="checkbox" name="State" lay-text="在职|离职" value="1" checked lay-skin="switch">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">系统账号</label>
                <div class="layui-input-block">
                    <input id="LoginName" type="text" name="LoginName" required lay-verify="required" autocomplete="off" class="layui-input" disabled>
                </div>
            </div>
            
            <button id="submit" lay-submit lay-filter="submit" style="display:none;"></button>
        </div>

        <div class="edit-submit">
            <button id="truebutton" class="layui-btn layui-btn-lg">提交</button>
        </div>
    </div>
</body>
</html>
<script>
    layui.use(['layer', 'form'], function () {
        var layer = layui.layer,
            form = layui.form;

        var type = MyPublic.getUrlParam('type');
        var id = MyPublic.getUrlParam('id');

        switch (type) {
            case 'add':
                $("#title").html('添加人员');
                
                break;
            case 'edit':
                $("#title").html('编辑人员');
                
                break;
        }
        GetUserGroupList(type, id);
        $("#truebutton").click(function () {
            $("#submit").click();
        })
        
        //添加或修改
        form.on('submit(submit)', function (data) {
            //console.log(data.field) //当前容器的全部表单字段，名值对形式：{name: value}
            var json = data.field;
            
            switch (type) {
                case 'add':
                    ajaxpost({
                        url: 'api/Method/UserInfoCreate?Token=' + MyPublic.getToken(),
                        data: json,
                        success: function (data) {
                            data = JSON.parse(data);
                            if (data.code === "10000") {
                                //当你在iframe页面关闭自身时
                                var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                                parent.layer.close(index); //再执行关闭   
                            }
                            else {
                                layer.msg(data.msg);
                            }
                        }
                    })

                    break;
                case 'edit':
                    json.UserCode = id;
                    ajaxpost({
                        url: 'api/Method/UserInfoUpdate?Token=' + MyPublic.getToken(),
                        data: json,
                        success: function (data) {
                            data = JSON.parse(data);
                            if (data.code === "10001") {
                                //当你在iframe页面关闭自身时
                                var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                                parent.layer.close(index); //再执行关闭   
                            }
                            else {
                                layer.msg(data.msg);
                            }
                        }
                    })
                    break;
            }
            
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });
        

    });
    /**
     * 获取用户分组
     */
    function GetUserGroupList(type,id) {
        layui.use(['layer', 'form'], function () {
            var layer = layui.layer,
                form = layui.form;
            ajaxget({
                url: 'api/Method/UserGroupList?Token=' + MyPublic.getToken(),
                success: function (data) {
                    var lis = [];
                    layui.each(data, function (index, item) {
                        lis.push('<option value="' + item.GroupCode + '">' + item.ChinaName + '</option>');
                    })
                    var datahtml = lis.join('');

                    $("#GroupList").append(datahtml);
                    form.render('select');
                    if (type == "edit") {
                        BindDataModel(id);
                    }
                }
            })
            
        });
    }
    function BindDataModel(guid) {
        layui.use(['layer', 'form'], function () {
            var layer = layui.layer,
                form = layui.form;

            ajaxget({
                url: 'api/MethodPublic/UserInfoModel?UserCode=' + guid,
                success: function (data) {
                    $("#Name").val(data.Name);
                    $("#Phone").val(data.Phone);
                    $("#LoginName").val(data.Phone);
                    $("#GroupList").val(data.GroupCode);
                    if (data.Sex === 0) {
                        $("#Sex").attr('checked', false)
                    }
                    if (data.State === 0) {
                        $("#State").attr('checked', false)
                    }
                    form.render('select');
                    form.render('checkbox');
                }
            })
        });
    }
    function FnPhone() {
        var phone = $("#Phone").val();
        $("#LoginName").val(phone);
    }
</script>